import { BasicColumn, FormSchema } from "/@/components/Table"
import { rules } from "/@/utils/helper/validator"
import { render } from "/@/utils/common/renderUtils"
import { getAllRolesList } from "/@/views/system/tenantMember/user.api"
import { DescItem } from "/@/components/Description/index"
import { h } from "vue"
import OpenSecretSettingvue from "./components/OpenSecretSetting.vue"
import * as timeUtils from "/@/utils/timeUtils"

export const detailSchema: DescItem[] = [
  {
    field: "name",
    label: "客户端名称",
  },
  {
    field: "clientId",
    label: "客户端ID",
  },
  {
    field: "clientSecret",
    label: "客户端密钥",
  },
  {
    field: "version",
    label: "客户端版本",
  },
  {
    field: "secretSetting",
    label: "加密/签名Key",
    render: (curVal, data) => {
      return data["isSettingSecret"] ? h(OpenSecretSettingvue, { record: data }) : render.renderTag("未设置", "red")
    },
  },
  {
    field: "authorizedGrantTypesText",
    label: "授权模式",
  },
  {
    field: "permPolicyList",
    label: "权限策略",
    render: (curVal, data) => {
      return data["roleList"]
        ? render.renderTag(
            data["permPolicyList"].map((e) => e.name),
            "green"
          )
        : ""
    },
  },
  {
    field: "scope",
    label: "作用域",
    render: (curVal, data) => {
      return curVal ? curVal.join("、") : ""
    },
  },
  {
    field: "accessTokenValidity",
    label: "令牌失效",
    render: (curVal, data) => {
      const { days, hours, minutes } = timeUtils.secondsToDaysHoursMinutes(curVal)
      return `${days}天${hours}时${minutes}分`
    },
  },
  {
    field: "refreshTokenValidity",
    label: "刷新失效",
    render: (curVal, data) => {
      const { days, hours, minutes } = timeUtils.secondsToDaysHoursMinutes(curVal)
      return `${days}天${hours}时${minutes}分`
    },
  },
  {
    field: "enableSign",
    label: "开启接口签名",
    render: (curVal, data) => {
      return render.renderTag(curVal === '1' ? '是' : '否', curVal === '1' ? "green" : "orange")
    },
  },
  {
    field: "ipChangeResetSession",
    label: "Ip改变重置会话",
    render: (curVal, data) => {
      return render.renderTag(curVal === '1' ? '是' : '否', curVal === '1' ? "green" : "orange")
    },
  },
  
  {
    field: "autoApproveText",
    label: "自动放行",
    render: (curVal, data) => {
      return render.renderTag(curVal, data["autoApprove"] == "1" ? "green" : "orange")
    },
  },
  {
    field: "canSameTimeOnlineText",
    label: "允许同时在线",
    render: (curVal, data) => {
      return render.renderTag(curVal, data["canSameTimeOnline"] == "1" ? "green" : "orange")
    },
  },
  {
    field: "useCaptchaText",
    label: "使用验证码",
    span: 4,
    render: (curVal, data) => {
      return render.renderTag(curVal, data["useCaptcha"] == "1" ? "green" : "orange")
    },
  },
  // {
  //   field: 'status',
  //   label: '状态',
  //   render: (curVal, data) => {
  //     const color = curVal == '1' ? 'red' : curVal == '0'? 'green': 'gray';
  //     return render.renderTag(render.renderDict(curVal, 'login_status'),color)
  //   },
  // },
  {
    field: "remark",
    label: "备注",
    span: 4,
  },
  {
    field: "Divider_LoginFail",
    label: "",
    span: 4,
    render: (curVal, data) => {
      return render.renderDividerWithValue("连续登录失败设置")
    },
  },
  {
    field: "loginFailCount",
    label: "登录失败次数",
  },
  {
    field: "lockInterval",
    label: "锁定周期",
    render: (curVal, data) => {
      return curVal ? curVal + " 秒" : ""
    },
  },
  {
    field: "lockMultiplier",
    label: "锁定时间乘子",
  },
]

export const extLdpColumns: BasicColumn[] = [
  {
    width: 120,
    dataIndex: "grantType",
    ifShow: false,
  },
  {
    title: "名称",
    width: 120,
    dataIndex: "name",
  },

  {
    title: "状态",
    width: 64,
    dataIndex: "checked",
    slot: "checked",
  },
]

export const columns: BasicColumn[] = [
  {
    title: "客户端名称",
    width: 120,
    dataIndex: "name",
  },
  {
    title: "客户端ID",
    width: 150,
    dataIndex: "clientId",
  },
  {
    title: "版本",
    width: 90,
    dataIndex: "version",
  },

  {
    title: "域",
    width: 100,
    dataIndex: "scope",
    customRender: ({ text }) => {
      return text ? text.join("|") : ""
    },
  },
  {
    title: "自动放行",
    width: 100,
    dataIndex: "autoApprove",
    customRender: ({ text }) => {
      return text ? "是" : "否"
    },
  },
  {
    title: "令牌失效",
    width: 110,
    dataIndex: "accessTokenValidity",
    customRender: ({ text }) => {
      const { days, hours, minutes } = timeUtils.secondsToDaysHoursMinutes(text)
      return `${days}天${hours}时${minutes}分`
    },
  },
  {
    title: "刷新失效",
    width: 110,
    dataIndex: "refreshTokenValidity",
    customRender: ({ text }) => {
      const { days, hours, minutes } = timeUtils.secondsToDaysHoursMinutes(text)
      return `${days}天${hours}时${minutes}分`
    },
  },
  {
    title: "创建时间",
    width: 160,
    dataIndex: "createdAt",
  },
]

export const historyColumns: BasicColumn[] = [
  {
    title: "客户端名称",
    width: 120,
    dataIndex: "name",
  },
  {
    title: "客户端ID",
    width: 120,
    dataIndex: "clientId",
  },
  {
    title: "版本",
    width: 90,
    dataIndex: "version",
  },

  {
    title: "创建时间",
    width: 160,
    dataIndex: "createdAt",
  },
  {
    title: "更新时间",
    width: 160,
    dataIndex: "updatedAt",
  },
]

export const searchFormSchema: FormSchema[] = [
  {
    field: "clientId",
    label: "客户端ID",
    component: "Input",
    colProps: { span: 8 },
  },
]

export const formSchema: FormSchema[] = [
  {
    field: "id",
    label: "id",
    component: "Input",
    show: false,
  },
  {
    field: "isDetail",
    label: "isDetail",
    component: "Input",
    show: false,
  },
  {
    field: "name",
    label: "客户端名称",
    component: "Input",
    required: true,
  },
  {
    field: "clientId",
    label: "客户端ID",
    component: "Input",
    required: true,
    ifShow: ({ values }) => {
      return !!values.id
    },
    dynamicDisabled: ({ values }) => {
      return !!values.id
    },
  },
  {
    field: "clientSecret",
    label: "客户端密钥",
    component: "Input",
    required: true,
    componentProps: {
      placeholder: "请输入客户端密钥",
      maxlength: 60,
      showCount: true,
    },
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
  {
    field: "version",
    label: "客户端版本",
    component: "Input",
    required: true,
    ifShow: ({ values }) => {
      return !!values.id
    },
    componentProps: {
      readonly: true,
    },
  },
  {
    field: "isSettingSecret",
    label: "是否设置过密钥",
    defaultValue: false,
    component: "RadioButtonGroup",
    show: false,
  },
  {
    field: "secretSetting",
    label: "加密/签名Key",
    component: "Input",
    ifShow: ({ values }) => {
      return !!values.id
    },
    slot: "secretSetting",
  },
  {
    field: "authorizedGrantTypes",
    label: "授权模式",
    required: true,
    component: "JDictSelectTag",
    componentProps: {
      mode: "multiple",
      dictType: "authorized_grant_types",
      placeholder: "请选择授权模式",
    },
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
  {
    field: "scope",
    label: "域",
    defaultValue: "server",
    component: "Input",
    required: true,
    componentProps: {
      placeholder: "请输入域",
    },
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },

  {
    field: "accessTokenValidity",
    label: "令牌失效",
    defaultValue: "604800",
    component: "Input",
    required: true,
    componentProps: {
      placeholder: "请输入令牌失效",
    },
    slot: "accessTokenValidity",
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },

  {
    field: "refreshTokenValidity",
    label: "刷新失效",
    defaultValue: "2592000",
    component: "Input",
    required: true,
    slot: "refreshTokenValidity",
    componentProps: {
      placeholder: "请输入刷新失效",
    },
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },

  {
    field: "enableSign",
    label: "开启接口签名",
    required: true,
    component: "JDictSelectTag",
    defaultValue: '1',
    componentProps: {
      dictType: "true_flase",
      type: "radio"
    },
  },
  {
    // 开启后, 如果请求Ip和登录Ip不一致会要求强制登录
    field: "ipChangeResetSession",
    label: "Ip改变重置会话",
    required: true,
    component: "JDictSelectTag",
    defaultValue: '1',
    componentProps: {
      dictType: "true_flase",
      type: "radio"
    },
  },
  
  {
    field: "autoApprove",
    label: "自动放行",
    required: true,
    component: "JDictSelectTag",
    defaultValue: "1",
    componentProps: {
      dictType: "true_flase",
      type: "radio",
      dropdownStyle: {
        maxHeight: "6vh",
      },
    },
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
  {
    field: "canSameTimeOnline",
    label: "允许同时在线",
    defaultValue: "1",
    required: true,
    component: "JDictSelectTag",
    componentProps: {
      dictType: "true_flase",
      type: "radio",
      dropdownStyle: {
        maxHeight: "6vh",
      },
    },
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
  {
    field: "useCaptcha",
    label: "使用验证码",
    defaultValue: "1",
    required: true,
    component: "JDictSelectTag",
    componentProps: {
      dictType: "true_flase",
      type: "radio",
      dropdownStyle: {
        maxHeight: "6vh",
      },
    },
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },

  {
    field: "remark",
    label: "备注",
    component: "InputTextArea",
    required: false,
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
  {
    field: "divider-api-select",
    component: "Divider",
    label: "连续登录失败设置",
  },
  {
    field: "loginFailCount",
    label: "登录失败次数",
    defaultValue: "-1",
    helpMessage: "当登录失败达到一定次数之后, 就会被锁定, -1不限制",
    component: "Input",
    required: false,
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
  {
    field: "lockInterval",
    label: "锁定周期",
    helpMessage: "锁定周期(单位为秒)",
    defaultValue: "10",
    component: "Input",
    required: false,
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
  {
    field: "lockMultiplier",
    label: "锁定时间乘子",
    defaultValue: "1",
    helpMessage: "下一次锁定的时间 = 连续被锁定次数 * 锁定周期 * 锁定时间乘子",
    component: "Input",
    required: false,
    dynamicDisabled: ({ values }) => {
      return values.isDetail === "1"
    },
  },
]

export const secretFormSchema: FormSchema[] = [
  {
    field: "id",
    label: "id",
    component: "Input",
    show: false,
  },
  {
    field: "clientId",
    label: "客户端ID",
    component: "Input",
    required: true,
    dynamicDisabled: ({ values }) => {
      return true
    },
  },
  {
    field: "asymmetryCryptoType",
    label: "非对称加密类型",
    defaultValue: "RSA",
    helpMessage: [
      "对称加密公钥, 比如前端在js中存放公钥(混淆后)",
      "页面在启动时候会根据clientId获取aes密钥key和签名key(经过私钥加密)",
      "然后部分接口就可以使用aes加密传输, 且效率比rsa加密快, 前端签名key使用signKey",
    ],
    component: "JDictSelectTag",
    componentProps: {
      dictType: "asymmetryCryptoType",
      type: "radio",
    },
  },
  {
    field: "publicKey",
    label: "公钥Key",
    component: "InputTextArea",
    required: true,
    ifShow: ({ values }) => {
      return !!values.publicKey
    },
    componentProps: {
      autoSize: {
        minRows: 6,
        maxRows: 6,
      },
      readOnly: true,
    },
  },

  {
    field: "privateKey",
    label: "私钥Key",
    helpMessage: "提供给调用方, 一般用于加密获取密钥数据(签名Key, Aes加密Key等)",
    component: "InputTextArea",
    required: false,
    ifShow: ({ values }) => {
      return !!values.privateKey
    },
    componentProps: {
      autoSize: {
        minRows: 6,
        maxRows: 6,
      },
      readOnly: true,
    },
  },

  {
    field: "symmetryCryptoType",
    label: "对称加密类型",
    defaultValue: "AES",
    component: "JDictSelectTag",
    componentProps: {
      dictType: "symmetryCryptoType",
      type: "radio",
    },
  },

  {
    field: "secretKey",
    label: "加密密钥Key",
    component: "InputTextArea",
    required: false,
    ifShow: ({ values }) => {
      return !!values.secretKey
    },
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 3,
      },
      readOnly: true,
    },
  },

  {
    field: "signKey",
    label: "签名Key",
    component: "InputTextArea",
    required: false,
    ifShow: ({ values }) => {
      return !!values.signKey
    },
    componentProps: {
      autoSize: {
        minRows: 3,
        maxRows: 3,
      },
      readOnly: true,
    },
  },
]
